package cn.hhchat.wp.enums;

import cn.hhchat.wp.exception.InValidFieldsException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created this one by HMH on 2017/10/27.
 */
@Getter
@AllArgsConstructor
public enum ConfigEnum {

    allowReg("是否允许注册"),
    allowLogin("是否允许登陆")
    ;

    String description;

    private static final Map<String, ConfigEnum> stringToEnum = new HashMap<>();

    static {
        for (ConfigEnum e : values()) {
            stringToEnum.put(e.name(), e);
        }
    }

    public static ConfigEnum fromString(String name, String fieldName) {
        ConfigEnum result = stringToEnum.get(name);
        if (result == null) {
            throw new InValidFieldsException(fieldName + "枚举字段错误");
        }
        return result;
    }

    public static List<String> list() {
        return new ArrayList<>(stringToEnum.keySet());
    }


}
